package zct.proj.util;

import java.util.Date;
import java.util.Iterator;
import java.util.Map.Entry;

import zct.proj.customView.StationDisplay;
import zct.proj.pojo.EquipDC;
import zct.proj.pojo.EquipXHJ;
import zct.proj.pojo.EquipmentInfo;
import zct.proj.pojo.Switcher;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;

public class DrawBitmapThread implements Runnable {

	StationDisplay sd;
	private boolean needRedraw;
	private boolean run = true;
	private boolean canRedraw = true;
	
	public void end() {
		run = false;
	}
	
	public DrawBitmapThread(StationDisplay sd) {
		this.sd = sd;
	}
	
	public void stopDrawing() {
		canRedraw = false;
//		Log.v("drawthread", "set draw off----------");
	}
	
	public void startDrawing() {
		canRedraw = true;
//		Log.v("drawthread", "set draw on...........");
	}
	
	public void setNeedRedraw() {
		needRedraw = true;
	}
	
	@Override
	public void run() {
		CommUtil.writeLog("绘图线程启动。");
		while (run && !Thread.interrupted()) {
			if (needRedraw && canRedraw) {
				drawStationMap();
				needRedraw = false;
			}
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		CommUtil.writeLog("绘图线程终止。");
	}

	public void drawStationMap() {
		long t1=0,t2=0,t3=0,t4=0,t5=0;

//		Bitmap img = Bitmap.createBitmap(sd.drawUtil.getCacheBitmap());
		t1 = new Date().getTime();
//    	long tt1 = new Date().getTime();
    	sd.drawUtil.redrawAll(sd.stationInfo.ctlList);
		Bitmap img = sd.drawUtil.getCacheBitmap();
//		long tt2 = new Date().getTime();
//    	Log.i("sd", "++++++++++++++++draw controls cost time : " + (tt2 - tt1));
		Canvas canvas = new Canvas(img);
		t2 = new Date().getTime();
		for (EquipmentInfo ep : sd.stationInfo.getCtlList()) {
			
			if (ep.getSwMap() == null) {
				continue;
			}
			
			if (ep.getName().equals(Const.EQUI_DC)) {
				EquipDC dc = (EquipDC) ep;
				int color = Const.COLOR_DEACTIVE;
				
				Iterator<Entry<String, Integer>> itsw = dc.getSwMap().entrySet().iterator();
				while (itsw.hasNext()) {
					Entry<String, Integer> entry = itsw.next();
					String keyTmp = entry.getKey();
					Switcher swTmp = sd.stationInfo.switcherMap.get(keyTmp);
					if (swTmp.status == 1) {
						if (entry.getValue() == Const.SW_DC_CINDEX_BEF_RED) {
							// 红白同高时优先显示红灯
							color = StationDisplay.getDrawColor(ep.getName(), entry.getValue());
							break;
						} else if (entry.getValue() == Const.SW_DC_CINDEX_BEF_WHITE) {
							color = StationDisplay.getDrawColor(ep.getName(), entry.getValue());
						}
//						Log.v("draw change", "dc sw:"+keyTmp+"");
					}
				}
				
				sd.drawUtil.drawSbDC(dc, color, canvas);
			} else if (ep.getName().equals(Const.EQUI_WCQD)) {
				Iterator<Entry<String, Integer>> itsw = ep.getSwMap().entrySet().iterator();
				int color = Const.COLOR_DEACTIVE;
				while (itsw.hasNext()) {
					Entry<String, Integer> entry = itsw.next();
					String keyTmp = entry.getKey();
					Switcher swTmp = sd.stationInfo.switcherMap.get(keyTmp);
					if (swTmp.status == 1) {
						if (entry.getValue() == Const.SW_WCQD_CINDEX_RED) {
							color = Color.RED;
							break;
						} else {
							color = Color.WHITE;
						}
					}
				}
//				color = getDrawColor(ep.getName(), entry.getValue());
				sd.drawUtil.drawSbWCQD(ep, color, canvas);
			} else {
				Iterator<Entry<String, Integer>> itsw = ep.getSwMap().entrySet().iterator();
				while (itsw.hasNext()) {
					Entry<String, Integer> entry = itsw.next();
					String keyTmp = entry.getKey();
					Switcher swTmp = sd.stationInfo.switcherMap.get(keyTmp);
//					if (ep.getDisp().equals("2/4") || keyTmp.equals(tp3)) {
//						Log.v("md drawchange", "before draw sw : " + tp3 + ",status:" + swTmp.status);
//					}
					if (swTmp.status == 1) {
						int color = 0;
						if (ep.getName().equals(Const.EQUI_XHJ)) {
							if (ep.getDisp().equals("X6")) {
								color = 0;
							}
		//					Log.v("draw lamp", "High at : " + entry.getKey() + ", color index:" + st.colorIndex);
							color = StationDisplay.getDrawColor(ep.getName(), ep.getType(), entry.getValue());
							EquipXHJ ex = (EquipXHJ) ep;
							if (entry.getValue() == Const.SW_XHJ_CINDEX_YELLOW && ex.lampCount == 2) {
								sd.drawUtil.drawSbXHJLamp2(ex, color, canvas);
							} else {
								sd.drawUtil.drawSbXHJLamp1(ex, color, canvas);
							}
						} else if (ep.getName().equals(Const.EQUI_DHAN)) {
							if (ep.getType() != Const.TYPE_BTN_DCBS) {
								color = StationDisplay.getDrawColor(ep.getName(), entry.getValue());
								sd.drawUtil.drawSbDHANLamp(ep, color, canvas);
							} else {
								if (entry.getValue() == Const.TYPE_BTN_DC_FIX) {
//									Log.v("draw dcbs", ep.getDisp() + " : green");
									color = Color.GREEN;
									sd.drawUtil.drawSbDHANLamp(ep, color, canvas);
								} else if (entry.getValue() == Const.TYPE_BTN_DC_OPP) {
//									Log.v("draw dcbs", ep.getDisp() + " : yellow");
									color = Color.YELLOW;
									sd.drawUtil.drawSbDHANLamp(ep, color, canvas);
								}
							}
							
		//					Log.v("md", "draw wcqd with color:"+color);
						}
					}
				}
			}
		}
		t3 = new Date().getTime();
		for (Integer idx : sd.stationInfo.xhjIdxList) {
			EquipmentInfo ei = sd.stationInfo.ctlList.get(idx);
//			if (ei.getType() == Const.TYPE_XHJ_CFDDAZ
//					|| ei.getType() == Const.TYPE_XHJ_CFDDGZ
//					|| ei.getType() == Const.TYPE_XHJ_DCAZ
//					|| ei.getType() == Const.TYPE_XHJ_DCGZ) {
				// 单灯信号机需重新计算颜色，当所有开关量均为低时，显示红灯
				Iterator<Entry<String, Integer>> itsw = ei.getSwMap().entrySet().iterator();
				boolean skipRed = false;;
				while (itsw.hasNext()) {
					Entry<String, Integer> entry = itsw.next();
					String keyTmp = entry.getKey();
					Switcher swTmp = sd.stationInfo.switcherMap.get(keyTmp);
					if (swTmp.status == 1) {
						skipRed = true;
						break;
					}
				}
				
				if (skipRed) {
					continue;
				}
				// 信号机显示红灯，调车信号机显示蓝灯
				int color = Color.RED;
				if (ei.getType() == Const.TYPE_XHJ_DCAZ
					|| ei.getType() == Const.TYPE_XHJ_DCGZ) {
					color = Color.BLUE;
				}
				
				EquipXHJ ex = (EquipXHJ) ei;
				sd.drawUtil.drawSbXHJLamp1(ex, color, canvas);
//				Log.v("StaDis", "Draw red color for single xhj:"+ei.getDisp());
//			}
		}
		t4 = new Date().getTime();
		Iterator<Entry<String, String>> it = sd.warningSwCtlList.entrySet().iterator();
		while (it.hasNext()) {
			Entry<String, String> entry = it.next();
			String keyTmp = entry.getKey();
			int color = Color.WHITE;
			if (keyTmp.endsWith("H")) {
				color = Color.RED;
			}
			
			String[] ary = entry.getValue().split(",");
			for (String a : ary) {
				EquipmentInfo ei = sd.stationInfo.ctlList.get(Integer.valueOf(a));
				// 多选时可能出现重复的区段编号，在此检测剔除
				if (ei.getSwMap().get(keyTmp) != null) {
					sd.drawUtil.drawSbWCQDMark(ei, canvas, color);
				}
			}
		}
		t5 = new Date().getTime();
		if (sd.warningSelIdx != null) {
			String[] ary = sd.warningSelIdx.split(",");
			for (String a : ary) {
				sd.drawUtil.drawSbWCQDRect(sd.stationInfo.ctlList.get(Integer.valueOf(a)), canvas, Color.YELLOW);
			}
		}
		long t6 = new Date().getTime();
		sd.mainImage.changeImage(img);
		long t7 = new Date().getTime();
		canvas = null;
		redraw();
		long t8 = new Date().getTime();

		Log.i("StationDisplay", "time spend thread:"+(t2-t1)+","+(t3-t2)+","+(t4-t3)+","+(t5-t4)+","+(t6-t5)+","+(t7-t6)+","+(t8-t7));
	}
	
	private void redraw() {
		Canvas canvas = sd.sh.lockCanvas();
		if (canvas != null) {
			canvas.drawColor(Color.BLACK);
			sd.mainImage.draw(canvas);
			sd.sh.unlockCanvasAndPost(canvas);
		}
	}
}
